*-------------------------------------------------------------------------------------------------------*
*RESEARCHERS:		Mette Foged, Linea Hasager and Vasil Yasenov
*PROJECT:			Meta-analysis
*DESCRIPTION:		1  Load database
*					2  Generate additional variables
*					3  Run robustness checks of the results
*-------------------------------------------------------------------------------------------------------*

clear all
set more off

global x_ctr			eu restworld
global x_macro	 		Unemployment GDPgrowth
global x_outputgap		OutputGap GDPgrowth
global x_skillratio2	ratio_f_n
global x_study 			i.method i.data_frequency i.skill
global x_study2 		i.method i.data_frequency i.skill i.outcome_t


use "${data}/database_clean_robustness.dta", replace

gen index_avg=index
gen pc1_avg=pc1
gen index_cc=index
gen pc1_cc=pc1

*RENAME, BUT NOT UNEMPLOYMENT, GDP GROWTH AND OUTPUT GAP
foreach var of varlist AverageTenure CollBarg StrictEmpColl StrictEmpIndColl StrictEmpInd StrictEmpTemp TradeUnionDens MintoMedianWage MinToMeanWage ALMPExp BTE BTTI StateControl PMR NetReplacementRate index pc1 {
rename `var' `var'_avg_avg
rename `var'_cc `var'_avg_avg_cc
}




*-------------------------------------------------------------------------------------------------------*
*-------------------------------------------------------------------------------------------------------*

destring wage empl, replace

* Rest of the world
gen restworld=0
replace restworld=1 if eu==0 & northamerica==0

*Nordic countries
gen nordic=0
replace nordic=1 if country == "Norway" | country == "Sweden" |country == "Denmark" |country == "Finland"
	
*Rest of Europe
gen resteu=0
replace resteu=1  if country=="Austria" | country=="France" | country=="Germany" | country=="Greece" ///
| country=="Ireland" | country=="Italy" | country=="Netherlands" | country=="Portugal" | country=="Spain" ///
| country=="Switzerland" | country=="United Kingdom" | country=="Western Europe" | country=="European Economic Area" | country=="Albania"

tab region*
replace region="Rest of Europe" if resteu==1
replace region="Nordic countries" if nordic==1



replace wage = 0 if wage == .
tab wage empl, m

replace skill="All" if skill=="all"

gen yearfe = int(firstyear/10) * 10
gen pubyearfe = int(year/10)*10


gen top100="Not top 100"
replace top100="Top 100" if journal_ranking < 101

gen top50="Not top 50"
replace top50="Top 50" if journal_ranking < 51

replace method="Simulation" if method=="simulation"

gen avg=1 if variation=="Area"
replace avg=0 if variation=="Skill" | variation=="Mixture"

gen refugee="Refugees" if refugees==1
replace refugee="Workers, mixture or other" if refugees==0


* Weights
gen p_se=1/se
gen p_score=1/Score



* Distinguish relative versus average effects
tab variation
gen relative=0
replace relative=1 if variation=="Skill" | variation=="Mixture" 
replace relative=. if variation !="Skill" & variation!="Mixture" & variation !="Area"
gen area=0
replace area=1 if variation=="Area"



egen cluster2 = group(country yearfe)
gen cluster=paper_id



local empl_txt "Employment Effects"
local wage_txt "Wage Effects"

*generate numeric variables that can be used as factors

tab method
gen method_org=method
gen method_d=0
replace method_d=1 if inlist(method,"IV","DIDIV","FDIV")
drop method
rename method_d method

tab data_frequency
gen data_t=0
replace data_t=1 if data_frequency=="Annual or more frequent"
drop data_frequency
rename data_t data_frequency

tab skill
gen skill_t=0
replace skill_t=1 if skill=="All"
replace skill_t=2 if skill=="Low"
replace skill_t=3 if skill=="High"
drop skill
rename skill_t skill


*Split outcome in wages and employment
gen 	outcome_empl="Employment Rate" 				if outcome=="e01"
replace outcome_empl="Unemployment Rate" 			if outcome=="u01"
replace outcome_empl="Labor Force Participation" 	if outcome=="p"
replace outcome_empl="Other Employment Variable"	if outcome=="e"

gen 	outcome_empl2=outcome_empl

gen 	outcome_wage="Hourly Wage" 					if outcome=="w"
replace	outcome_wage="Daily Wage"		 			if outcome=="wxday"
replace	outcome_wage="Monthly Wage"		 			if outcome=="wxmonth"
replace	outcome_wage="Quarterly Wage"	 			if outcome=="wxquarter"
replace	outcome_wage="Unclear Wage Variable"		if outcome=="wxunclear"
replace	outcome_wage="Weekly Wage"		 			if outcome=="wxweek"
replace	outcome_wage="Yearly Wage"		 			if outcome=="wxyear"

gen 	outcome_wage2="Hourly, Daily, Weekly Wage"	 	if inlist(outcome_wage,"Hourly Wage","Daily Wage","Weekly Wage")
replace	outcome_wage2="Monthly, Quarterly, Yearly Wage"	if inlist(outcome_wage,"Monthly Wage","Quarterly Wage","Yearly Wage")
replace	outcome_wage2="Unclear Wage Variable"			if inlist(outcome_wage,"Unclear Wage Variable")


*Factor variables for outcome types
gen		outcome_t=1
replace	outcome_t=0 if  outcome_wage2=="Hourly, Daily, Weekly Wage" | outcome_empl=="Employment Rate"
replace outcome_t=2 if  outcome_empl=="Unemployment Rate"


*Dummy for dropping OLS if there are IV estimates within study
gen iv=0
replace iv=1 if inlist(method_org,"IV","DIDIV","FDIV")
gen ols=0
replace ols=1 if inlist(method_org,"OLS","DID","FD")



*SEPARATE EMPLOYMENT AND WAGE EFFECTS
foreach type in empl wage {

bysort paper_id `type': egen _iv_`type'=sum(iv)

gen _keep_`type'=1
replace _keep_`type'=0 if ols==1 &  _iv_`type'>0
drop _iv_`type'



*-------------------------------------------------------------------------------------------------------*
* TABLE : MAIN RESULTS
*-------------------------------------------------------------------------------------------------------*


label variable StrictEmpInd_avg "\emph{EPL (Regular)}"
label variable CollBarg_avg "\emph{Collective Bargaining}"
label variable AverageTenure_avg "\emph{Average Job Tenure}"
label variable ALMPExp_avg "\emph{ALMP}"
label variable StateControl_avg "\emph{siness Regulation}"
label variable StrictEmpTemp_avg "\emph{EPL (Temporary)}"
label variable StrictEmpColl_avg "\emph{EPL (Collective)}"
label variable BTTI_avg "\emph{Barriers Trade and Investment}"
label variable BTE_avg "\emph{Barriers Entrepreneurship}"
label variable PMR_avg "\emph{Product Market Regulation}"
label variable NetReplacementRate_avg "\emph{Net Replacement Rate}"
label variable MintoMedianWage_avg "\emph{Minimum/Median Wage}"
label variable index_avg "\emph{Index}"
label variable pc1_avg "\emph{Index (PCA)}"



local inst StrictEmpInd_avg
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N

bysort paper_id `type' relative inst _keep_`type': gen p_N_3a = 1/_N

gen inst_avg=1 if  `inst'_avg!=. & se!=.
bysort paper_id `type' relative inst_avg: gen p_N_avg = 1/_N 

bysort paper_id `type' relative inst preferred_spec: gen p_N_pref = 1/_N 

gen se_not1=1
replace se_not1=0 if se>1 & se !=.
bysort paper_id `type' relative inst se_not1: gen p_N_4 = 1/_N 


eststo clear
preserve
keep if `type'==1
eststo est2: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1  & (se  <=1 | se==.) [pw=p_se*p_score*p_N_4], vce(cluster cluster)
eststo est5: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5a: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 & _keep_`type'==1 [pw=p_se*p_score*p_N_3a], vce(cluster cluster)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg `inst'
eststo est5b: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
preserve
keep if `type'==1
eststo est5d: qui reg estimate `inst' $x_ctr $x_macro  if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5e: qui reg estimate `inst' $x_ctr  $x_study  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5f: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1 & preferred_spec==1 [pw=p_se*p_score*p_N_pref], vce(cluster cluster)
eststo est5g: qui reg estimate `inst' $x_macro  $x_study  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5h: qui reg estimate `inst' $x_ctr  $x_study $x_outputgap if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg_cc `inst'
eststo est5i: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
preserve
keep if `type'==1
eststo est5j: qui reg estimate `inst' $x_ctr $x_macro  $x_study $x_skillratio2 if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5k: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 & excl_firm_industry!=1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5l: qui reg estimate `inst' $x_ctr $x_macro  $x_study2  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est7: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1  & (se  <=1 | se==.) [pw=p_se*p_score*p_N_4], vce(cluster cluster)
eststo est10: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est11: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 & _keep_`type'==1 [pw=p_se*p_score*p_N_3a], vce(cluster cluster)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg `inst'
eststo est12: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
preserve
keep if `type'==1
eststo est14: qui reg estimate `inst' $x_ctr $x_macro  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est15: qui reg estimate `inst' $x_ctr $x_study  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est16: qui reg estimate `inst' $x_ctr $x_macro  $x_study if area==1 & preferred_spec==1 [pw=p_se*p_score*p_N_pref], vce(cluster cluster)
eststo est17: qui reg estimate `inst' $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est18: qui reg estimate `inst' $x_ctr $x_study  $x_outputgap if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg_cc `inst'
eststo est19: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
preserve
keep if `type'==1
eststo est20: qui reg estimate `inst' $x_ctr $x_macro $x_study $x_skillratio2 if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est21: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 & excl_firm_industry!=1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est22: qui reg estimate `inst' $x_ctr $x_macro  $x_study2  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
restore

drop inst p_N p_N_3 p_N_3a p_N_4 se_not1 p_N_avg inst_avg p_N_pref


*Table
esttab est5 est5k est5a est5d est5e est5g est10 est21 est11 est14 est15 est17 using $out/appendix_table2_`type'.tex, /// 
	keep(`inst') /// 
	replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label noobs ///
	nonum nonotes  nogaps postfoot("") posthead("\midrule") nolines prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{6}{c}|cccccc}\toprule") ///
	mgroups("Panel A: Relative" "Panel B: Total", pattern(1 0 0 0 0 0 1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(1)" "(2)" "(3)" "(4)" "(5)" "(6)") 
	
	
**********
**********
	
foreach inst in StrictEmpColl_avg AverageTenure_avg CollBarg_avg NetReplacementRate_avg {
eststo clear
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N 

bysort paper_id `type' relative inst _keep_`type': gen p_N_3a = 1/_N

gen inst_avg=1 if  `inst'_avg!=. & se!=.
bysort paper_id `type' relative inst_avg: gen p_N_avg = 1/_N 

bysort paper_id `type' relative inst preferred_spec: gen p_N_pref = 1/_N 

gen se_not1=1
replace se_not1=0 if se>1 & se !=.
bysort paper_id `type' relative inst se_not1: gen p_N_4 = 1/_N 



eststo clear
preserve
keep if `type'==1
eststo est2: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1  & (se  <=1 | se==.) [pw=p_se*p_score*p_N_4], vce(cluster cluster)
eststo est5: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5a: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 & _keep_`type'==1 [pw=p_se*p_score*p_N_3a], vce(cluster cluster)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg `inst'
eststo est5b: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
preserve
keep if `type'==1
eststo est5d: qui reg estimate `inst' $x_ctr $x_macro  if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5e: qui reg estimate `inst' $x_ctr  $x_study  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5f: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1 & preferred_spec==1 [pw=p_se*p_score*p_N_pref], vce(cluster cluster)
eststo est5g: qui reg estimate `inst' $x_macro  $x_study  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5h: qui reg estimate `inst' $x_ctr  $x_study  $x_outputgap if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg_cc `inst'
eststo est5i: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
preserve
keep if `type'==1
eststo est5j: qui reg estimate `inst' $x_ctr $x_macro  $x_study $x_skillratio2 if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5k: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1 & excl_firm_industry!=1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est5l: qui reg estimate `inst' $x_ctr $x_macro  $x_study2  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est7: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if area==1  & (se  <=1 | se==.) [pw=p_se*p_score*p_N_4], vce(cluster cluster)
eststo est10: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est11: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if area==1 & _keep_`type'==1 [pw=p_se*p_score*p_N_3a], vce(cluster cluster)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg `inst'
eststo est12: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
preserve
keep if `type'==1
eststo est14: qui reg estimate `inst' $x_ctr $x_macro   if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est15: qui reg estimate `inst' $x_ctr $x_study  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est16: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if area==1 & preferred_spec==1 [pw=p_se*p_score*p_N_pref], vce(cluster cluster)
eststo est17: qui reg estimate `inst' $x_macro  $x_study  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est18: qui reg estimate `inst' $x_ctr $x_study $x_outputgap if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg_cc `inst'
eststo est19: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
preserve
keep if `type'==1
eststo est20: qui reg estimate `inst' $x_ctr $x_macro  $x_study $x_skillratio2 if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est21: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if area==1 & excl_firm_industry!=1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo est22: qui reg estimate `inst' $x_ctr $x_macro  $x_study2  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
restore



drop inst p_N p_N_3 p_N_3a p_N_4 se_not1 p_N_avg inst_avg p_N_pref


*Table
esttab est5  est5k est5a est5d est5e est5g est10 est21 est11 est14 est15 est17 using $out/appendix_table2_`type'.tex, ///
	keep(`inst') ///
	append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label noobs ///
	nonum nonotes  nogaps postfoot("") prehead("") nolines ///
mtitles("" "" "" "" "" "" "" "" "" "" "" "")
}


**********
**********/

local inst index_avg
eststo clear
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N 

bysort paper_id `type' relative inst _keep_`type': gen p_N_3a = 1/_N

gen inst_avg=1 if  `inst'_avg!=. & se!=.
bysort paper_id `type' relative inst_avg: gen p_N_avg = 1/_N 

bysort paper_id `type' relative inst preferred_spec: gen p_N_pref = 1/_N 

gen se_not1=1
replace se_not1=0 if se>1 & se !=.
bysort paper_id `type' relative inst se_not1: gen p_N_4 = 1/_N 


preserve
keep if `type'==1
eststo est2: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 & (se  <=1 | se==.) [pw=p_se*p_score*p_N_4], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est5: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est5a: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1 & _keep_`type'==1  [pw=p_se*p_score*p_N_3a], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg `inst'
eststo est5b: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1  [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
preserve
keep if `type'==1
eststo est5d: qui reg estimate `inst' $x_ctr $x_macro  if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "No"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est5e: qui reg estimate `inst' $x_ctr $x_study if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "No"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est5f: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 & preferred_spec==1 [pw=p_se*p_score*p_N_pref], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est5g: qui reg estimate `inst' $x_macro $x_study if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "No"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est5h: qui reg estimate `inst' $x_ctr $x_study $x_outputgap if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "No"
estadd local outputgap "X"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg_cc `inst'
eststo est5i: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1  [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
preserve
keep if `type'==1
eststo est5j: qui reg estimate `inst' $x_ctr $x_macro $x_study $x_skillratio2 if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "X"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est5k: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if relative==1 & excl_firm_industry!=1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est5l: qui reg estimate `inst' $x_ctr $x_macro $x_study2  if relative==1  [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "X"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est7: qui reg estimate `inst' $x_ctr $x_macro  $x_study  if area==1 & (se  <=1 | se==.) [pw=p_se*p_score*p_N_4], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est10: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est11: qui reg estimate `inst' $x_ctr $x_macro  $x_study if area==1 & _keep_`type'==1 [pw=p_se*p_score*p_N_3a], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "X"
estadd local n=round(e(N), .0)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg `inst'
eststo est12: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
preserve
keep if `type'==1
eststo est14: qui reg estimate `inst' $x_ctr $x_macro if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "No"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est15: qui reg estimate `inst' $x_ctr $x_study if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "No"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est16: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 & preferred_spec==1 [pw=p_se*p_score*p_N_pref], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est17: qui reg estimate `inst' $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "No"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est18: qui reg estimate `inst' $x_ctr $x_study $x_outputgap if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "No"
estadd local outputgap "X"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
restore
preserve
keep if `type'==1
drop `inst'
rename `inst'_avg_cc `inst'
eststo est19: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_avg], vce(cluster cluster)
restore
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
preserve
keep if `type'==1
eststo est20: qui reg estimate `inst' $x_ctr $x_macro $x_study $x_skillratio2 if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "X"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est21: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 & excl_firm_industry!=1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "No"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
eststo est22: qui reg estimate `inst' $x_ctr $x_macro $x_study2 if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local outputgap "No"
estadd local study "X"
estadd local study2 "X"
estadd local skill "No"
estadd local refugees "No"
estadd local n=round(e(N), .0)
restore



*Table
esttab est5 est5k est5a est5d est5e  est5g est10  est21 est11 est14 est15 est17 using $out/appendix_table2_`type'.tex, ///
	keep(`inst') ///
	scalars( "regionfe \midrule Region FE"  "macro Country-Level Controls"  "study Study Characteristics") ///
	append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label noobs ///
	nonum nonotes nogaps prehead("") posthead("\hline \\") nolines postfoot("\bottomrule \end{tabular}}") ///
	mtitles("" "" "" "" "" "" "" "" "" "" "" "")

	drop inst p_N p_N_3 p_N_3a p_N_4 se_not1 p_N_avg inst_avg p_N_pref
}
